package com.quanshu.exchange.support.net.core;

import android.support.annotation.NonNull;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.quanshu.exchange.support.config.Config;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 网络辅助类
 */
public class NetClient {
    public static volatile NetClient instance;
    private final OkHttpClient okHttpClient;
    private final Map<Class<?>, Object> map = new HashMap();
    private final Map<Class<?>, Object> map2 = new HashMap();
    private Retrofit netRetrofit;
    private Retrofit shopRetrofit;
//    private Retrofit tencentRetrofit;

    public static NetClient getInstance() {
        //第一层避免不必要的线程同步
        if (instance == null) {
            synchronized (NetClient.class) {
                //第二层为空则创建实例
                if (instance == null) {
                    instance = new NetClient();
                }
            }
        }
        return instance;
    }

    /**
     * 创建网络辅助类
     */
    public NetClient() {
        okHttpClient = createOkHttpClient();
        initNetInterface();
        initShopInterface();
//        initTencentInterface();
    }

    /**
     * 创建OkHttpClient客户端
     *
     * @return
     */
    @NonNull
    public OkHttpClient createOkHttpClient() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
//                .addInterceptor(new ChuckInterceptor(MyApplication.getInstance()))
                .addInterceptor(new HeaderInterceptor())
                .addInterceptor(logging)
                .build();
    }

    /**
     * 初始化网络接口的Retrofit
     */
    public void initNetInterface() {
        netRetrofit = new Retrofit.Builder()
                .baseUrl(Config.URL_HOST_NET)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 初始化网络接口的Retrofit
     */
    public void initShopInterface() {
        shopRetrofit = new Retrofit.Builder()
                .baseUrl(Config.URL_HOST_SHOP)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

//    /**
//     * 初始化腾讯接口的Retrofit
//     */
//    public void initTencentInterface() {
//        tencentRetrofit = new Retrofit.Builder()
//                .baseUrl("https://api.weixin.qq.com")
//                .client(new OkHttpClient())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//    }

    /**
     * 动态获取接口实现类
     */
    public <T> T getShopService(Class<T> clazz) {
        T t = (T) map2.get(clazz);
        if (t == null) {
            t = shopRetrofit.create(clazz);
            map2.put(clazz, t);
        }
        return t;
    }

    /**
     * 动态获取接口实现类
     */
    public <T> T getNetService(Class<T> clazz) {
        T t = (T) map.get(clazz);
        if (t == null) {
            t = netRetrofit.create(clazz);
            map.put(clazz, t);
        }
        return t;
    }

//    /**
//     * 动态获取接口实现类
//     */
//    public <T> T getTencentService(Class<T> clazz) {
//        T t = (T) map.get(clazz);
//        if (t == null) {
//            t = tencentRetrofit.create(clazz);
//            map.put(clazz, t);
//        }
//        return t;
//    }

}
